<template>
  <div class="page">
    <el-card class="card">
      <div class="user-info">
        <div class="header">
          <h1>用户信息中心</h1>
        </div>

        <div class="info-item">
          <el-icon class="icon"><User /></el-icon>
          <span class="label">用户名：</span>
          <span class="value">{{ userInfo.username }}</span>
        </div>

        <div class="info-item">
          <el-icon class="icon"><Lock /></el-icon>
          <span class="label">用户权限：</span>
          <el-tag :type="userInfo.isAdmin ? 'danger' : 'success'">
            {{ userInfo.isAdmin ? '管理员' : '普通用户' }}
          </el-tag>
        </div>
      </div>

      <el-divider border-style="dashed" />

      <div class="password-form">
        <h2>修改密码</h2>
        <el-form :model="pwdForm" :rules="rules" ref="pwdFormRef">
          <el-form-item prop="oldPassword">
            <el-input
                v-model="pwdForm.oldPassword"
                placeholder="请输入旧密码"
                type="password"
                clearable
                prefix-icon="Lock"
            />
          </el-form-item>

          <el-form-item prop="newPassword">
            <el-input
                v-model="pwdForm.newPassword"
                placeholder="请输入新密码（至少6位）"
                type="password"
                clearable
                prefix-icon="Key"
            />
          </el-form-item>

          <el-form-item prop="confirmPassword">
            <el-input
                v-model="pwdForm.confirmPassword"
                placeholder="请确认新密码"
                type="password"
                clearable
                prefix-icon="Check"
            />
          </el-form-item>

          <el-button
              type="primary"
              @click="handleSubmit"
              class="submit-btn">
            立即修改
          </el-button>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup>
import {ref, reactive, onMounted} from 'vue'
import {ElMessage} from 'element-plus'
import {User, Lock, Key, Check} from '@element-plus/icons-vue'
import axios from "axios";

// 用户信息
const userInfo = reactive({
  username: '',
  isAdmin: false
})

// 密码表单
const pwdForm = reactive({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

// 表单验证规则
const rules = {
  oldPassword: [
    {required: true, message: '请输入旧密码', trigger: 'blur'}
  ],
  newPassword: [
    {required: true, message: '请输入新密码', trigger: 'blur'},
    {min: 6, message: '密码长度至少6位', trigger: 'blur'}
  ],
  confirmPassword: [
    {
      validator: (rule, value, callback) => {
        if (value !== pwdForm.newPassword) {
          callback(new Error('两次输入密码不一致'))
        } else {
          callback()
        }
      }, trigger: 'blur'
    }
  ]
}

// 初始化用户信息
onMounted(() => {
  userInfo.username = localStorage.getItem('username') || '未登录用户'
  userInfo.isAdmin = localStorage.getItem('isAdmin') === 'true'
})

// 提交修改
const handleSubmit = async() => {
  // 这里添加实际修改密码逻辑
  try {
    const { data } = await axios.post("/users/change-password",{
      password: pwdForm.confirmPassword
    },{
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${localStorage.getItem('token')}`
      }

    })
    ElMessage({
      message: data.message,
      type: 'success',
    });
  } catch (e) {
    await Promise.reject(e)
  }
  // 清空表单
  Object.keys(pwdForm).forEach(key => pwdForm[key] = '')
}
</script>

<style scoped>
.page {
  min-height: 100vh;
  width: 100vw;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  margin: 0;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(135deg, #409EFF 0%, #36D1DC 100%);
}
.card {
  margin-top: 2%;
  width: 60%;
  height: 90%;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
}

.user-info, .password-form {
  padding: 20px 40px;
}

.header {
  text-align: center;
  margin-bottom: 30px;
}

.header h1 {
  color: #409EFF;
  font-size: 24px;
  letter-spacing: 2px;
}

.info-item {
  display: flex;
  align-items: center;
  margin: 20px 0;
  font-size: 16px;
}

.icon {
  font-size: 20px;
  margin-right: 15px;
  color: #666;
}

.label {
  color: #666;
  min-width: 80px;
  margin-right: 15px;
}

.value {
  color: #333;
  font-weight: 500;
}

.password-form h2 {
  color: #67C23A;
  margin-bottom: 25px;
  border-left: 4px solid #67C23A;
  padding-left: 10px;
}

.submit-btn {
  width: 100%;
  margin-top: 15px;
  letter-spacing: 2px;
  font-weight: bold;
}

.el-divider {
  margin: 20px 0;
}
</style>